﻿using PipeDetection.Model;
using PipeDetection.Repository;
using PipeDetection.UI.接口;
using PipeDetection.UI.缺陷;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PipeDetection.UI
{
    public partial class FrmDetectionManager : UIPage,Ipage
    {
    
  
        public FrmDetectionManager()
        {
            InitializeComponent();
        }

        public event EventHandler PropertyChanged;

        public void InitD(object obj)
        {
            
        }

        private void FrmDetectionManager_Load(object sender, EventArgs e)
        {
            LoadData();
        }
        private void LoadData()
        {
            uiDataGridView1.DataSource = Globel.DefectCatalogRepository.Query();
        }

        private void uiSymbolButton1_Click(object sender, EventArgs e)
        {
            if (Globel.GetDlgResult(this,new FrmNewDetection(), out object result))
            {
                LoadData();
            }
        }

        private void uiSymbolButton3_Click(object sender, EventArgs e)
        {
            ((UIForm)this.ParentForm).RemovePage(this.PageIndex);
        }

        private void uiDataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            DefectCatalog defect=((List<DefectCatalog>)uiDataGridView1.DataSource)[e.RowIndex];
            defect= Globel.DefectCatalogRepository.QueryByKey(defect.Id);
            pictureBox1.Image = Common.DrawSymbol(defect);
        }

        private void uiSymbolButton4_Click(object sender, EventArgs e)
        {
            DefectCatalog defect = ((List<DefectCatalog>)uiDataGridView1.DataSource)[uiDataGridView1.SelectedIndex];
            if (Globel.DefectOservationsRepository.Query(x => x.DefectCatalogId == defect.Id).Count > 0)
            {
                UIMessageBox.ShowInfo("当前分类已被使用，不能删除！");
                return;
            }
            Globel.DefectCatalogRepository.DeleteByKey(defect.Id);
            LoadData();
        }

        private void uiSymbolButton2_Click(object sender, EventArgs e)
        {
            DefectCatalog defect = ((List<DefectCatalog>)uiDataGridView1.DataSource)[uiDataGridView1.SelectedIndex];
            new FrmNewDetection(defect).ShowDialog();
            LoadData();
        }
    }
}
